    <!-- 推荐歌单区域(非登录模式) -->
<template>
  <div>

    <!-- 首页轮播图区域开始 -->
    <el-carousel :interval="4000" type="card" height="350px">
      <el-carousel-item v-for="(item, i) in img" :key="i">
        <el-image :src="item.imageUrl" fit="fill" style="width: 100%; height: 100%" @click="getUrl(item)">
          <div slot="placeholder" class="image-slot">
            <el-image style="width: 100%; height: 100%" :src="require('../assets/image/hot50.jpg')" fit='fill'>
            </el-image>
          </div>
        </el-image>
        <el-tag :type="type[i]">
          {{ tags[i] }}
        </el-tag>
      </el-carousel-item>
    </el-carousel>
    <div class="titleHead">
      <div style="color: #ec4141">推荐歌单</div>
      <div @click="more()" class="more">换一批</div>
    </div>

    <hr />
    <el-row :gutter="12" style="margin-bottom: 40px">
      <el-col :md="6" :sm="12" :xs="24" v-for="(item, i) in personalizedData" :key="i" style="margin-bottom: 10px">
        <el-card shadow="hover" :body-style="{ padding: '0px' }">
          <div class="count">
            <i class="el-icon-video-play"></i>{{
                item.playCount >= 10000
                  ? (item.playCount / 10000).toFixed(0) + "万"
                  : item.playCount
            }}
          </div>
          <el-image :src="item.picUrl" fit="cover" @click="getItem(item.id)" lazy>
            <div slot="placeholder" class="image-slot">
              <el-image :src="require('../assets/image/hot50.jpg')" style="height: 258px" lazy></el-image>
            </div>
          </el-image>
        </el-card>
        <div class="title">{{ item.name }}</div>
      </el-col>
    </el-row>

    <Daily />
    <DailySong />

    <div class="loading" v-if="loading === true">
      <i class="el-icon-loading"></i>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapActions } from 'vuex';

export default {
  components: {
    Daily: () => import('./DailyRecomm.vue'),
    DailySong: () => import('./DailySong.vue'),
  },
  data() {
    return {
      bannersData: null,
      img: [],
      tags: [],
      type: [],
      personalized: [],
      number: 4,
      personalizedData: [],
      count: 0,
      loading: false,
      nowSong: [],
      id: null,
    };
  },
  mounted() {
    // 获取轮播图
    this.getBanners();
    // 获取推荐歌单
    this.getSongsList();
  },

  beforeRouteLeave(to, from, next) {
    this.loading = true
    next()
  },

  methods: {
    ...mapActions(['saveSongList', 'clickPlay', 'saveNowSongCount', 'saveNowSong']),
    // 获取轮播图
    async getBanners() {
      this.img = [];
      this.tags = [];
      this.type = [];
      this.$api.banner().then((ret) => {
        if (ret.code !== 200) {
          this.$message.error('超时获取失败!!!');
        } else {
          this.bannersData = ret.banners;
          this.bannersData.map((item, i) => {
            this.img.push(item);
            this.tags.push(item.typeTitle);
          });
          this.tags.map((item, i) => {
            if (item === '数字专辑') {
              this.type.push('');
            } else if (item === '新歌首发') {
              this.type.push('warning');
            } else {
              this.type.push('danger');
            }
          });
        }
      });
    },
    // 获取推荐歌单
    async getSongsList() {
      this.personalized = [];
      this.$api.personalized().then((ret) => {
        if (ret.code !== 200) {
          this.$message.error('超时获取失败!!!');
        } else {
          ret.result.map((item, i) => {
            this.personalized.push({
              id: item.id,
              picUrl: item.picUrl,
              name: item.name,
              copywriter: item.copywriter,
              playCount: item.playCount,
            });
          });
          this.count = this.personalized.length;
          this.personalizedData = this.personalized.slice(0, this.number);
        }
      });
    },

    // 查看更多功能
    more() {
      this.number += 4;
      this.personalizedData = [];
      this.personalizedData = this.personalized.slice(
        this.number,
        this.number + 4,
      );
      if (this.number > this.count) {
        this.personalizedData = [];
        this.number = 4;
        this.personalizedData = this.personalized.slice(0, this.number);
      }
    },
    // 跳转到歌单明细
    async getItem(e) {
      this.loading = true;
      this.$api.getPlayList({ id: e, timestamp: this.$utils.timestamp(), cookie: localStorage.getItem('cookie') }).then((ret) => {
        if (ret.code === 200) {
          this.saveSongList({ songList: ret.playlist }); // 获取到的明细存入VUEX
          this.$router.push(`/songList?id=${e}`);
          this.clickPlay({ ifPlay: true });
        } else {
          this.$message.error('超时获取失败!!!');
        }
      });
    },

    getUrl(e) {
      const cookie = localStorage.getItem('cookie');
      if (e.targetId !== 0) {
        this.$api.musicItem({ ids: e.targetId, cookie: localStorage.getItem('cookie') }).then((ret) => {
          ret.songs.map((item, i) => {
            this.nowSong.push(item)
          })
          this.unique()
          this.saveNowSongCount({ nowSongCount: this.nowSong });
        })
        this.$api.songUrl({ cookie, id: e.targetId }).then((ret) => {
          for (let i = 0; i < this.nowSong.length; i++) {
            if (this.nowSong[i].id === e.targetId) {
              const title = this.nowSong[i].name;
              const arr = this.nowSong[i].ar.map((item1) => {
                return item1.name;
              });
              const artist = arr.join();
              const pic = this.nowSong[i].al.picUrl;
              this.id = this.nowSong.length
              for (let i = 0; i < this.nowSong.length; i++) {
                if (this.nowSong[i].id === ret.data[0].id) {
                  this.id = i
                  break
                }
              }
              if (ret.data[0].url !== null) {
                this.saveNowSong({
                  id: this.id,
                  title,
                  artist,
                  src: ret.data[0].url,
                  pic,
                  songId: ret.data[0].id,
                });
                this.$api.scrobble({ id: ret.data[0].id, cookie: cookie, sourceid: this.nowSong[i].al.id, timestamp: this.$utils.timestamp() })
                this.clickPlay({ ifPlay: false });
              } else {
                this.$message.error('该歌曲暂时无法播放!!!');
              }
              break
            }
          }
        });
      } else {
        this.$message({
          message: '跳转到购买页面',
          type: 'success',
        });
        window.open(e.url, '_blank') // 在新窗口打开外链接
      }
    },

    // 去重
    unique() {
      const hash = {};
      this.nowSong = this.nowSong.reduce((preVal, curVal) => {
        hash[curVal.id] ? '' : hash[curVal.id] = true && preVal.push(curVal);
        return preVal
      }, [])
    },
  },
};
</script>

<style scoped>
.titleHead {
  display: flex;
  justify-content: space-between;
  font-size: 25px;
}

.el-image {
  position: relative;
  width: 100%;
  height: 100%;
}

.el-tag {
  position: absolute;
  z-index: 999;
  right: 0px;
  bottom: 0px;
}

.more {
  color: #ec4141;
  text-align: center;
  cursor: pointer;
}

.el-col {
  overflow: hidden;
}

.el-card {
  position: relative;
}

.title {
  font-weight: 700;
  color: #ec4141;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.count {
  position: absolute;
  z-index: 10;
  color: #fff;
  background-image: linear-gradient(#080808, #aca5a538);
  width: 100%;
  height: 20px;
  text-align: right;
}

.loading {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
}

.el-icon-loading {
  position: fixed;
  font-size: 50px;
  left: 50%;
  top: 50%;
  color: #ec4141;
}

.el-carousel {
  z-index: 0;
}
</style>
